﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.Collections.ObjectModel;
using System.Text;
using Microsoft.Phone.Shell;

namespace unifiqAnthony
{
    public partial class ViewEvent : PhoneApplicationPage
    {
        WebClient client;

        public ViewEvent()
        {
            InitializeComponent();
            Loaded += MainPage_Loaded;

        }

        private void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            string url = "http://unifiq.5kportal.com/retrieve_bing_event.php";
            Uri uri = new Uri(url);
            client = new WebClient();
            client.AllowReadStreamBuffering = true;

            client.DownloadStringCompleted += client_DownloadStringCompleted;
            client.DownloadProgressChanged += client_DownloadProgressChanged;

            client.DownloadStringAsync(uri);
        }

        private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Cancelled == false & e.Error == null)
            {
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
                ObservableCollection<Event> list = new ObservableCollection<Event>();
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ObservableCollection<Event>));
                list = (ObservableCollection<Event>)serializer.ReadObject(ms);

                List<Event> eventList = new List<Event>();

                foreach (Event evt in list)
                {
                    eventList.Add(new Event(evt.id, evt.creatorID, evt.name, evt.dateTime, evt.groupID, evt.latitude, evt.longitude, evt.description));
                }

                List<Event> pastList = new List<Event>();
                List<Event> presentList = new List<Event>();
                List<Event> futureList = new List<Event>();

               for (int i = 0; i < eventList.Count; i++)
                {
                    int diff = eventList[i].dateTime.Date.CompareTo(DateTime.Now.Date);
                    if (diff < 0)
                    {
                        pastList.Add(eventList[i]);
                    }
                    else if (diff == 0)
                    {
                        presentList.Add(eventList[i]);
                    }
                    else if (diff > 0)
                    {
                        futureList.Add(eventList[i]);
                    }
                }

               past_lb.ItemsSource = pastList;
               present_lb.ItemsSource = presentList;
               future_lb.ItemsSource = futureList;

            }

        }

        private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {

        }

        private void lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {              
                Event selectedEvent = (Event)e.AddedItems[0];
                Uri targetPage = new Uri("/ViewEventDetail.xaml", UriKind.Relative);
                ((ListBox)sender).SelectedIndex = -1;
                PhoneApplicationService.Current.State["selectedEvent"] = selectedEvent;
                NavigationService.Navigate(targetPage);
            }

        }

    }

    [DataContract]
    public class Event
    {
        [DataMember]
        public int id { get; set; }

        [DataMember]
        public int creatorID { get; set; }

        [DataMember]
        public string name { get; set; }

        [DataMember]
        public DateTime dateTime { get; set; }

        [DataMember]
        public int groupID { get; set; }

        [DataMember]
        public double latitude { get; set; }

        [DataMember]
        public double longitude { get; set; }

        [DataMember]
        public string description { get; set; }

        public Event(int id, int creatorID, string name, DateTime dateTime, int groupID, double latitude, double longitude, string description)
        {
            this.id = id;
            this.creatorID = creatorID;
            this.name = name;
            this.dateTime = dateTime;
            this.groupID = groupID;
            this.latitude = latitude;
            this.longitude = longitude;
            this.description = description;
        }

    }
}